package com.glory.dmegcpda.bean;

import android.content.Context;

import com.glory.dmegcpda.base.MyApp;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;

import java.io.Serializable;
import java.util.ArrayList;

import androidx.fragment.app.FragmentManager;

public class BaseResponseBean<T> implements Serializable {

    private ReturnBean<T> Response;

    private transient FreeUI_GeneralFragmentDialog tipDialog;


    public ReturnBean<T> getResponse() {
        return Response;
    }

    public void setResponse(ReturnBean<T> response) {
        this.Response = response;
    }

    //获取到对象后，必须要判断是否获取成功
     public boolean isSuccess(Context context, boolean isHaveBody, FragmentManager fragmentManager) {
         if (Response.getHeader().getRESULT().toUpperCase().equals("SUCCESS")) {
             if (isHaveBody) {
                 if (Response.getBody() == null || Response.getBody().equals("")) {
                     showFailDialog("暂无请求内容",fragmentManager);
                     return false;
                 } else {
                     if (Response.getBody() instanceof ArrayList) {
                         if ((((ArrayList) Response.getBody()).size()) > 0) {
                             return true;
                         } else {
                             showFailDialog("暂无请求数据",fragmentManager);
                             return false;
                         }
                     } else {
                         return true;
                     }
                 }
             } else {
                 return true;
             }
         } else {
             showFailDialog(Response.getHeader().getRESULTMESSAGE(),fragmentManager);
             return false;
         }
     }


     private void showFailDialog(String message, FragmentManager fragmentManager)
    {
        tipDialog = FreeApi_DialogUtils.getConfirmDialog(MyApp.app, "确定", message, () -> {

        });
        tipDialog.show(fragmentManager,"DIALOG");
    }

}
